﻿using System.Web;
using System.Web.Security;

/// <summary>
///     LoginUser 的摘要说明
/// </summary>
public class LoginUser
{
    private static readonly object syncRoot = new object();

    public static LoginUser Instance
    {
        get
        {
            var login = (LoginUser) HttpContext.Current.Cache["loginUser"]; //从缓存读取  
            if (login == null)
            {
                lock (syncRoot)
                {
                    if (login == null)
                    {
                        login = new LoginUser();
                        HttpContext.Current.Cache.Insert("loginUserClass", login);
                    }
                }
            }
            return login;
        }
    }

    /// <summary>
    ///     用户登陆ID
    /// </summary>
    public string UserID
    {
        get
        {
            var o = HttpContext.Current.Session["UserID"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["UserID"] = value; }
    }

    /// <summary>
    ///     用户姓名
    /// </summary>
    public string UserName
    {
        get
        {
            var o = HttpContext.Current.Session["UserName"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["UserName"] = value; }
    }

    /// <summary>
    ///     真实姓名
    /// </summary>
    public string RealName
    {
        get
        {
            var o = HttpContext.Current.Session["RealName"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["RealName"] = value; }
    }

    /// <summary>
    ///     邮箱
    /// </summary>
    public string Email
    {
        get
        {
            var o = HttpContext.Current.Session["Email"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["Email"] = value; }
    }

    /// <summary>
    ///     联系电话
    /// </summary>
    public string Tel
    {
        get
        {
            var o = HttpContext.Current.Session["Tel"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["Tel"] = value; }
    }

    /// <summary>
    ///     角色ID
    /// </summary>
    public string RoleID
    {
        get
        {
            var o = HttpContext.Current.Session["RoleID"];
            if (o == null)
            {
                FormsAuthentication.SignOut();
                ExitSys();
            }
            return o.ToString();
        }
        set { HttpContext.Current.Session["RoleID"] = value; }
    }

    public bool Online
    {
        get
        {
            var o = HttpContext.Current.Session["UserID"];
            return o == null ? false : true;
        }
    }

    /// <summary>
    ///     检查用户是否登陆
    /// </summary>
    public void ChkUserLogin()
    {
        if (Online == false)
        {
            FormsAuthentication.SignOut();
            ExitSys();
        }
    }

    public void ExitSys()
    {
        HttpContext.Current.Session.Abandon();
        HttpContext.Current.Response.Redirect("~/Default.aspx", true);
    }
}